package com.accp.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.accp.biz.VacationBiz;
import com.accp.pojo.Users;
import com.accp.pojo.Vacation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class VacationAction implements ModelDriven<Vacation> {
	private Vacation vacation = new Vacation();
	
	@Override
	public Vacation getModel() {
		return vacation;
	}
	
	private VacationBiz vacationBiz;
	
	public void setVacationBiz(VacationBiz vacationBiz) {
		this.vacationBiz = vacationBiz;
	}
	/**
	 * 经理查看休假信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String find(){
		List<Vacation> list = null;
		Map<String,Object> request = (Map<String,Object>)ActionContext.getContext().get("request");
		Map<String,Object> session = ActionContext.getContext().getSession();
		Users user = (Users) session.get("user");
		String checker =user.getUserName();
		String name=user.getUserName();
		if(user.getRole().equals("经理")){
			list = vacationBiz.findByManage(checker, name);
		}else if(user.getRole().equals("普通员工")){
			list = vacationBiz.findByEmployee(name);
		}
		if(null!=list){
			request.put("vacationList", list);
			return "find";
		}
		return "error";
	}
	/**
	 * 审核员工休假信息
	 * @return
	 */
	public String audit(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer flag = Integer.parseInt(request.getParameter("flag"));
		Integer id = vacation.getId();
		Integer state = vacation.getState();
		Vacation vacation = vacationBiz.findById(id);
		if(null!=vacation){
			if(state == 0 && flag == 1){
				vacation.setState(1);
				vacationBiz.updateaudit(vacation);
				return "audit";
			}else if(state == 0 && flag ==2){
				vacation.setState(2);
				vacationBiz.updateaudit(vacation);
				return "audit";	
			}
		}
		return "error";
	}
	/**
	 * 通过ID查看休假信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String findById(){
		Map<String,Object> request = (Map<String,Object>)ActionContext.getContext().get("request");
		Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
		Vacation vacation = vacationBiz.findById(id);
		if(null!=vacation){
			request.put("vacation", vacation);
			return "findById";
		}
		return "error";
	}
	/**
	 * 查找审核人（经理）
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String findManage(){
		Map<String,Object> request = (Map<String,Object>)ActionContext.getContext().get("request");
		List<String> list = vacationBiz.findManage();
		if(null!=list){
			request.put("manageList", list);
			return "findManage";
		}
		return "error";
	}
	/**
	 * 申请休假（增加）
	 * @return
	 */
	public String application(){
		//计算请假天数
		Integer days = (int) ((vacation.getEndTime().getTime()-vacation.getStartTime().getTime())/(3600*24*1000));
		vacation.setDays(days);
		vacationBiz.add(vacation);
		return "application";
	}
	/**
	 * 删除休假信息
	 * @return
	 */
	public String delete(){
		if(vacationBiz.delete(vacation)){
			return "delete";
		}
		return "error";
	}
	
	/**
	 * 分页查看休假信息
	 * 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String selectFirstPage(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		try{
			Map<String,Object> session = ActionContext.getContext().getSession();
			Users user = (Users) session.get("user");
			String checker =user.getUserName();
			String name=user.getUserName();
			List list = vacationBiz.selectByPage("Vacation", 1, 3,checker,name);
			int totalPage = vacationBiz.searchRecordsNextPage(3, "Vacation",checker,name);
			request.put("list", list);
			request.put("totalPage", totalPage);
			request.put("currentPage", 1);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectFirstPage";
	}
	/**
	 * 分页查找下一页
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String selectNextPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int next = Integer.parseInt(request.getParameter("page"));
		try{
			Map<String,Object> session = ActionContext.getContext().getSession();
			Users user = (Users) session.get("user");
			String checker =user.getUserName();
			String name=user.getUserName();
			List list = vacationBiz.selectByPage("Vacation", next, 3,checker,name);
			int total = vacationBiz.searchRecordsNextPage(3, "Vacation",checker,name);
			request.setAttribute("list", list);
			request.setAttribute("currentPage", next);
			request.setAttribute("totalPage", total);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectNextPage";
	}
}
